package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.classifier.structuredClassifier.Class;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.common.edit.command.SubsetAddCommand;
import org.eclipse.uml2.common.edit.command.SubsetSupersetReplaceCommand;
import org.eclipse.uml2.common.edit.command.SupersetRemoveCommand;
import org.eclipse.uml2.uml.AssociationClass;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;

public class AssociationClassItemProvider
		extends ClassItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public AssociationClassItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addRelatedElementPropertyDescriptor(object);
			addOwnedEndPropertyDescriptor(object);
			addMemberEndPropertyDescriptor(object);
			addIsDerivedPropertyDescriptor(object);
			addEndTypePropertyDescriptor(object);
			addNavigableOwnedEndPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addRelatedElementPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Relationship_relatedElement_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Relationship_relatedElement_feature", "_UI_Relationship_type"),
				UMLPackage.Literals.RELATIONSHIP__RELATED_ELEMENT, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addOwnedEndPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Association_ownedEnd_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Association_ownedEnd_feature", "_UI_Association_type"),
				UMLPackage.Literals.ASSOCIATION__OWNED_END, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addMemberEndPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Association_memberEnd_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Association_memberEnd_feature", "_UI_Association_type"),
				UMLPackage.Literals.ASSOCIATION__MEMBER_END, true, null, null, null));
	}

	protected void addIsDerivedPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Association_isDerived_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Association_isDerived_feature", "_UI_Association_type"),
				UMLPackage.Literals.ASSOCIATION__IS_DERIVED, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addEndTypePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Association_endType_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Association_endType_feature", "_UI_Association_type"),
				UMLPackage.Literals.ASSOCIATION__END_TYPE, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addNavigableOwnedEndPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Association_navigableOwnedEnd_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Association_navigableOwnedEnd_feature", "_UI_Association_type"),
				UMLPackage.Literals.ASSOCIATION__NAVIGABLE_OWNED_END, true, null, null, null));
	}

	@Override
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.ASSOCIATION__OWNED_END);
		}
		return childrenFeatures;
	}

	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/AssociationClass"));
	}

	@Override
	public String getText(Object object) {
		final String label = ((AssociationClass) object).getName();
		return label == null || label.length() == 0 ? getString("_UI_AssociationClass_type") :
				getString("_UI_AssociationClass_type") + " " + label;
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(AssociationClass.class)) {
			case UMLPackage.ASSOCIATION_CLASS__MEMBER_END:
			case UMLPackage.ASSOCIATION_CLASS__IS_DERIVED:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.ASSOCIATION_CLASS__OWNED_END:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ASSOCIATION__OWNED_END, UMLFactory.eINSTANCE.createProperty()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ASSOCIATION__OWNED_END, UMLFactory.eINSTANCE.createPort()));

		newChildDescriptors.add(createChildParameter(UMLPackage.Literals.ASSOCIATION__OWNED_END, UMLFactory.eINSTANCE.createExtensionEnd()));
	}

	@Override
	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.CLASSIFIER__OWNED_USE_CASE || childFeature == UMLPackage.Literals.CLASS__NESTED_CLASSIFIER || childFeature == UMLPackage.Literals.STRUCTURED_CLASSIFIER__OWNED_ATTRIBUTE || childFeature == UMLPackage.Literals.ASSOCIATION__OWNED_END
				|| childFeature == UMLPackage.Literals.BEHAVIORED_CLASSIFIER__OWNED_BEHAVIOR;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

	@Override
	protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection, int index) {
		if (feature == UMLPackage.Literals.ASSOCIATION__OWNED_END) {
			return new SubsetAddCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__MEMBER_END }, collection, index);
		}
		if (feature == UMLPackage.Literals.ASSOCIATION__NAVIGABLE_OWNED_END) {
			return new SubsetAddCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__OWNED_END }, collection, index);
		}
		return super.createAddCommand(domain, owner, feature, collection, index);
	}

	@Override
	protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) {
		if (feature == UMLPackage.Literals.ASSOCIATION__OWNED_END) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__NAVIGABLE_OWNED_END }, collection);
		}
		if (feature == UMLPackage.Literals.ASSOCIATION__MEMBER_END) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__OWNED_END }, collection);
		}
		return super.createRemoveCommand(domain, owner, feature, collection);
	}

	@Override
	protected Command createReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, Collection collection) {
		if (feature == UMLPackage.Literals.ASSOCIATION__OWNED_END) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__MEMBER_END }, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__NAVIGABLE_OWNED_END }, value, collection);
		}
		if (feature == UMLPackage.Literals.ASSOCIATION__NAVIGABLE_OWNED_END) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__OWNED_END }, null, value, collection);
		}
		if (feature == UMLPackage.Literals.ASSOCIATION__MEMBER_END) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.ASSOCIATION__OWNED_END }, value, collection);
		}
		return super.createReplaceCommand(domain, owner, feature, value, collection);
	}

}
